package tree;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Random;

import javax.swing.Timer;


public class Simulation extends Observable {

	private Timer timer;
	private Tree mainroad;
	private Random random;
	private int tt = 0;
	private final double startenergy = 150;
	private int sx = 500;
	private int sy = 800;
	
	public Simulation() {
		
		random = new Random();
		mainroad = new Tree(1, new Vector2d(sx,sy), new Vector2d(0, -1),startenergy,true);
		mainroad.generate(random);
		timer = new Timer(10, new TimeListener());
		timer.start();
	}

	public Tree getRoad() {
		return mainroad;
	}

	public void startstop() {
		if (timer.isRunning())
			timer.stop();
		else
			timer.start();
	}
	
	public void reset(){
		tt = 0;
		System.out.println("reset");
		mainroad = new Tree(1, new Vector2d(sx,sy), new Vector2d(0, -1),startenergy,true);
	}

	public void step() {
		//tt++;
		if(tt > 100)
			reset();
		//System.out.println("step");
		mainroad.step(random);
		//	reset();
		setChanged();
		notifyObservers();
	}

	private class TimeListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			step();
		}

	}

}
